/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.mrp.ppm.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import merlion.common.entity.Staff;

/**
 *
 * @author Zhu Xin, Zhang Ying
 */
@Entity
public class SOP implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String sopIdStr;  //The SOP with the same period got the same ID String
    private Timestamp dateCreated;
    private Integer startPeriodYear;
    private Integer startPeriodMonth;
    private Integer numOfMonth;
    private Boolean ableToEdit;
    
    private int sopIndex;
    
    @ManyToOne(targetEntity=Staff.class, cascade={CascadeType.MERGE},fetch=FetchType.EAGER)
    private Staff staff;
    
    @OrderBy("productIndex")
    @OneToMany(targetEntity=IndivSOP.class, cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    private Collection<IndivSOP> indivSOPs = new ArrayList<IndivSOP>();// size is 4 for 4 types, 0->A, 1->B, 2->C, 3->D

    public SOP(){
    }
    
    public SOP(int startYear, int startMonth, Timestamp currentTime){
        ableToEdit = true;
        numOfMonth = 3;
        for (int i = 0; i < 4; i++){
            // represent the 4 products
            IndivSOP temp = new IndivSOP();
            temp.setProductIndex(i);
            indivSOPs.add(temp);
        }
        // generate the sfIdStr
        sopIdStr = "SOP";
        if (startMonth == 1){
            sopIdStr = sopIdStr + "JanMar"+ startYear;
            sopIndex = 0;
        }else if (startMonth == 4){
            sopIdStr = sopIdStr + "AprJun"+ startYear;
            sopIndex = 1;
        } else if (startMonth == 7){
            sopIdStr = sopIdStr + "JulSep"+ startYear;
            sopIndex = 0;
        } else {
            sopIdStr = sopIdStr + "OctDec"+ startYear;
            sopIndex = 1;
        }
        // set datecreated
        dateCreated = currentTime;
        startPeriodMonth = new Integer(startMonth);
        startPeriodYear = new Integer(startYear);
    }
    
    public String getSOPPeriod(){
        if (startPeriodMonth.equals(1)){
            return "Jan - March, " + startPeriodYear;
        } else if (startPeriodMonth.equals(4)){
            return "April - Jun, " + startPeriodYear;
        } else if (startPeriodMonth.equals(7)){
            return "Jul - Sep, " + startPeriodYear;
        } else{
            return "Oct - Dec, " + startPeriodYear;
        }
    }
    
    public IndivSOP returnProductIndivSOP(int i){
        List<IndivSOP> temp = (List<IndivSOP>) indivSOPs;
        return temp.get(i);
    }
    
    public String getFormattedCreatedDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
        return sdf.format(dateCreated);
    }
    
    public String getFormattedDay(){
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        return sdf.format(dateCreated);
    }

    public Boolean getAbleToEdit() {
        return ableToEdit;
    }

    public void setAbleToEdit(Boolean ableToEdit) {
        this.ableToEdit = ableToEdit;
    }

    public int getSopIndex() {
        return sopIndex;
    }

    public void setSopIndex(int sopIndex) {
        this.sopIndex = sopIndex;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    public Timestamp getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Timestamp dateCreated) {
        this.dateCreated = dateCreated;
    }

    public Collection<IndivSOP> getIndivSOPs() {
        return indivSOPs;
    }

    public void setIndivSOPs(Collection<IndivSOP> indivSOPs) {
        this.indivSOPs = indivSOPs;
    }

    public Integer getNumOfMonth() {
        return numOfMonth;
    }

    public void setNumOfMonth(Integer numOfMonth) {
        this.numOfMonth = numOfMonth;
    }

    public String getSopIdStr() {
        return sopIdStr;
    }

    public void setSopIdStr(String sopIdStr) {
        this.sopIdStr = sopIdStr;
    }

    public Staff getStaff() {
        return staff;
    }

    public void setStaff(Staff staff) {
        this.staff = staff;
    }

    public Integer getStartPeriodMonth() {
        return startPeriodMonth;
    }

    public void setStartPeriodMonth(Integer startPeriodMonth) {
        this.startPeriodMonth = startPeriodMonth;
    }

    public Integer getStartPeriodYear() {
        return startPeriodYear;
    }

    public void setStartPeriodYear(Integer startPeriodYear) {
        this.startPeriodYear = startPeriodYear;
    }
    
    @Override
    public boolean equals(Object object) {
        if (!(object instanceof SOP)) {
            return false;
        }
        SOP other = (SOP) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.mrp.ppm.entity.SalesAndOperationPlanning[ id=" + id + " ]";
    }
    
}
